//
//  LYClaimController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/13.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYClaimController.h"
#import "LYCompanyCell.h"

@interface LYClaimController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *selectArray;

@end

@implementation LYClaimController

- (NSMutableArray *)selectArray {
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(0, 0, 0, 0.2);
    [self createSubview];
}

- (void)createSubview{
    CGFloat height = 0;
    if (self.itemDataArray && self.itemDataArray.count) {
        for (PublicCategoryElementModel *model in self.itemDataArray) {
            if (self.selectArray.count) {
                [self.selectArray addObject:@-1];
            } else {
                [self.selectArray addObject:@0];
            }
            
            height = height + 55.0;
            NSInteger count = model.childArray.count;
            if (count % 3 == 0) {
                height = height + count/3 * 45;
            } else {
                height = height + (count/3 + 1) * 45;
            }
        }
    }
    
    if (height > 350) {
        height = 350;
    }
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, height) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.bounces = NO;
    [self.view addSubview:self.tableview];
    [self.tableview registerClass:[LYCompanyCell class] forCellReuseIdentifier:kCellIdentifier_LYCompanyCell];
    
    UIButton *resetBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, EndY(self.tableview), WIDTH*0.5, 50) imageName:nil target:self action:@selector(resetBtnTap:) title:@"重置" titleColor:THEMECOLOR_OF_TEXT_666666];
    resetBtn.backgroundColor = COLOR_OF_WHITE;
    resetBtn.titleLabel.font = Font(Font_17);
    [resetBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    [self.view addSubview:resetBtn];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH/2, EndY(self.tableview), WIDTH*0.5, 50) imageName:nil target:self action:@selector(okBtnTap:) title:@"确定" titleColor:COLOR_OF_WHITE];
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.titleLabel.font = Font(Font_17);
    [okBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    [self.view addSubview:okBtn];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.itemDataArray && self.itemDataArray.count) {
        return self.itemDataArray.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYCompanyCell *cell =  [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYCompanyCell forIndexPath:indexPath];
    cell.backgroundColor = COLOR_OF_CLEAR;
    if (self.itemDataArray && self.itemDataArray.count) {
        [cell reloadData:self.itemDataArray[indexPath.row] selectIndex:[self.selectArray[indexPath.row] integerValue]];
        __weak typeof(self) weakSelf = self;
        cell.backlock = ^(NSInteger index){
            [weakSelf companyItemClick:indexPath.row subItem:index];
        };
    }
    return cell;
}

#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 55.0;
    if (self.itemDataArray && self.itemDataArray.count) {
        PublicCategoryElementModel *model = self.itemDataArray[indexPath.row];
        NSInteger count = model.childArray.count;
        if (count % 3 == 0) {
            height = height + count/3 * 45;
        } else {
            height = height + (count/3 + 1) * 45;
        }
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5.0;
}
#pragma mark-- Action
- (void)companyItemClick:(NSInteger)section subItem:(NSInteger)index{
    self.selectArray[section] = [NSNumber numberWithInteger:index];
    [self.tableview reloadData];
}

- (void)resetBtnTap:(UIButton *)button {
    [self.selectArray removeAllObjects];
    if (self.itemDataArray && self.itemDataArray.count) {
        for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
            [self.selectArray addObject:@-1];
        }
    }
    [self.tableview reloadData];
}
- (void)okBtnTap:(UIButton *)button {
    if (self.itemDataArray && self.itemDataArray.count) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
            PublicCategoryElementModel *model = self.itemDataArray[i];
            NSArray *dataArray = model.childArray;
            NSInteger index = [self.selectArray[i] integerValue];
            if (index >= 0) {
                [array addObject:dataArray[index]];
            }
        }
        if (array.count > 0) {
            self.backBlock(array);
        } else {
            self.backBlock(@[]);
        }
    } 
}

@end
